﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using PAT.Entities;
using PAT.Data;

#endregion

namespace PAT.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="PatMenuProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class PatMenuProviderBaseCore : EntityProviderBase<PAT.Entities.PatMenu, PAT.Entities.PatMenuKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIdRolFromPatRolMenu
		
		/// <summary>
		///		Gets PAT_MENU objects from the datasource by ID_ROL in the
		///		PAT_ROL_MENU table. Table PAT_MENU is related to table PAT_ROL
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="_idRol"></param>
		/// <returns>Returns a typed collection of PatMenu objects.</returns>
		public TList<PatMenu> GetByIdRolFromPatRolMenu(System.Int16 _idRol)
		{
			int count = -1;
			return GetByIdRolFromPatRolMenu(null,_idRol, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets PAT.Entities.PatMenu objects from the datasource by ID_ROL in the
		///		PAT_ROL_MENU table. Table PAT_MENU is related to table PAT_ROL
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idRol"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatMenu objects.</returns>
		public TList<PatMenu> GetByIdRolFromPatRolMenu(System.Int16 _idRol, int start, int pageLength)
		{
			int count = -1;
			return GetByIdRolFromPatRolMenu(null, _idRol, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatMenu objects from the datasource by ID_ROL in the
		///		PAT_ROL_MENU table. Table PAT_MENU is related to table PAT_ROL
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRol"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_MENU objects.</returns>
		public TList<PatMenu> GetByIdRolFromPatRolMenu(TransactionManager transactionManager, System.Int16 _idRol)
		{
			int count = -1;
			return GetByIdRolFromPatRolMenu(transactionManager, _idRol, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets PatMenu objects from the datasource by ID_ROL in the
		///		PAT_ROL_MENU table. Table PAT_MENU is related to table PAT_ROL
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRol"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_MENU objects.</returns>
		public TList<PatMenu> GetByIdRolFromPatRolMenu(TransactionManager transactionManager, System.Int16 _idRol,int start, int pageLength)
		{
			int count = -1;
			return GetByIdRolFromPatRolMenu(transactionManager, _idRol, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatMenu objects from the datasource by ID_ROL in the
		///		PAT_ROL_MENU table. Table PAT_MENU is related to table PAT_ROL
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="_idRol"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_MENU objects.</returns>
		public TList<PatMenu> GetByIdRolFromPatRolMenu(System.Int16 _idRol,int start, int pageLength, out int count)
		{
			
			return GetByIdRolFromPatRolMenu(null, _idRol, start, pageLength, out count);
		}


		/// <summary>
		///		Gets PAT_MENU objects from the datasource by ID_ROL in the
		///		PAT_ROL_MENU table. Table PAT_MENU is related to table PAT_ROL
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_idRol"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatMenu objects.</returns>
		public abstract TList<PatMenu> GetByIdRolFromPatRolMenu(TransactionManager transactionManager,System.Int16 _idRol, int start, int pageLength, out int count);
		
		#endregion GetByIdRolFromPatRolMenu
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, PAT.Entities.PatMenuKey key)
		{
			return Delete(transactionManager, key.IdMenu);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_idMenu">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int16 _idMenu)
		{
			return Delete(null, _idMenu);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenu">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int16 _idMenu);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_MENU_PAT_MENU key.
		///		FK_PAT_MENU_PAT_MENU Description: 
		/// </summary>
		/// <param name="_idMenuPadre"></param>
		/// <returns>Returns a typed collection of PAT.Entities.PatMenu objects.</returns>
		public TList<PatMenu> GetByIdMenuPadre(System.Int16? _idMenuPadre)
		{
			int count = -1;
			return GetByIdMenuPadre(_idMenuPadre, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_MENU_PAT_MENU key.
		///		FK_PAT_MENU_PAT_MENU Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenuPadre"></param>
		/// <returns>Returns a typed collection of PAT.Entities.PatMenu objects.</returns>
		/// <remarks></remarks>
		public TList<PatMenu> GetByIdMenuPadre(TransactionManager transactionManager, System.Int16? _idMenuPadre)
		{
			int count = -1;
			return GetByIdMenuPadre(transactionManager, _idMenuPadre, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_MENU_PAT_MENU key.
		///		FK_PAT_MENU_PAT_MENU Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenuPadre"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatMenu objects.</returns>
		public TList<PatMenu> GetByIdMenuPadre(TransactionManager transactionManager, System.Int16? _idMenuPadre, int start, int pageLength)
		{
			int count = -1;
			return GetByIdMenuPadre(transactionManager, _idMenuPadre, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_MENU_PAT_MENU key.
		///		fkPatMenuPatMenu Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idMenuPadre"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatMenu objects.</returns>
		public TList<PatMenu> GetByIdMenuPadre(System.Int16? _idMenuPadre, int start, int pageLength)
		{
			int count =  -1;
			return GetByIdMenuPadre(null, _idMenuPadre, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_MENU_PAT_MENU key.
		///		fkPatMenuPatMenu Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idMenuPadre"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT.Entities.PatMenu objects.</returns>
		public TList<PatMenu> GetByIdMenuPadre(System.Int16? _idMenuPadre, int start, int pageLength,out int count)
		{
			return GetByIdMenuPadre(null, _idMenuPadre, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_PAT_MENU_PAT_MENU key.
		///		FK_PAT_MENU_PAT_MENU Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenuPadre"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of PAT.Entities.PatMenu objects.</returns>
		public abstract TList<PatMenu> GetByIdMenuPadre(TransactionManager transactionManager, System.Int16? _idMenuPadre, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override PAT.Entities.PatMenu Get(TransactionManager transactionManager, PAT.Entities.PatMenuKey key, int start, int pageLength)
		{
			return GetByIdMenu(transactionManager, key.IdMenu, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_PAT_MENU index.
		/// </summary>
		/// <param name="_idMenu"></param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatMenu"/> class.</returns>
		public PAT.Entities.PatMenu GetByIdMenu(System.Int16 _idMenu)
		{
			int count = -1;
			return GetByIdMenu(null,_idMenu, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_MENU index.
		/// </summary>
		/// <param name="_idMenu"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatMenu"/> class.</returns>
		public PAT.Entities.PatMenu GetByIdMenu(System.Int16 _idMenu, int start, int pageLength)
		{
			int count = -1;
			return GetByIdMenu(null, _idMenu, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_MENU index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenu"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatMenu"/> class.</returns>
		public PAT.Entities.PatMenu GetByIdMenu(TransactionManager transactionManager, System.Int16 _idMenu)
		{
			int count = -1;
			return GetByIdMenu(transactionManager, _idMenu, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_MENU index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenu"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatMenu"/> class.</returns>
		public PAT.Entities.PatMenu GetByIdMenu(TransactionManager transactionManager, System.Int16 _idMenu, int start, int pageLength)
		{
			int count = -1;
			return GetByIdMenu(transactionManager, _idMenu, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_MENU index.
		/// </summary>
		/// <param name="_idMenu"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatMenu"/> class.</returns>
		public PAT.Entities.PatMenu GetByIdMenu(System.Int16 _idMenu, int start, int pageLength, out int count)
		{
			return GetByIdMenu(null, _idMenu, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_MENU index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenu"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatMenu"/> class.</returns>
		public abstract PAT.Entities.PatMenu GetByIdMenu(TransactionManager transactionManager, System.Int16 _idMenu, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#region CUST_PAT_MENU_GetMenuByUsuarioRol 
		
		/// <summary>
		///	This method wrap the 'CUST_PAT_MENU_GetMenuByUsuarioRol' stored procedure. 
		/// </summary>
		/// <param name="idUsuario"> A <c>System.Int32?</c> instance.</param>
		/// <param name="idRol"> A <c>System.Int32?</c> instance.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet GetMenuByUsuarioRol(System.Int32? idUsuario, System.Int32? idRol)
		{
			return GetMenuByUsuarioRol(null, 0, int.MaxValue , idUsuario, idRol);
		}
		
		/// <summary>
		///	This method wrap the 'CUST_PAT_MENU_GetMenuByUsuarioRol' stored procedure. 
		/// </summary>
		/// <param name="idUsuario"> A <c>System.Int32?</c> instance.</param>
		/// <param name="idRol"> A <c>System.Int32?</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet GetMenuByUsuarioRol(int start, int pageLength, System.Int32? idUsuario, System.Int32? idRol)
		{
			return GetMenuByUsuarioRol(null, start, pageLength , idUsuario, idRol);
		}
				
		/// <summary>
		///	This method wrap the 'CUST_PAT_MENU_GetMenuByUsuarioRol' stored procedure. 
		/// </summary>
		/// <param name="idUsuario"> A <c>System.Int32?</c> instance.</param>
		/// <param name="idRol"> A <c>System.Int32?</c> instance.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public DataSet GetMenuByUsuarioRol(TransactionManager transactionManager, System.Int32? idUsuario, System.Int32? idRol)
		{
			return GetMenuByUsuarioRol(transactionManager, 0, int.MaxValue , idUsuario, idRol);
		}
		
		/// <summary>
		///	This method wrap the 'CUST_PAT_MENU_GetMenuByUsuarioRol' stored procedure. 
		/// </summary>
		/// <param name="idUsuario"> A <c>System.Int32?</c> instance.</param>
		/// <param name="idRol"> A <c>System.Int32?</c> instance.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <remark>This method is generate from a stored procedure.</remark>
		/// <returns>A <see cref="DataSet"/> instance.</returns>
		public abstract DataSet GetMenuByUsuarioRol(TransactionManager transactionManager, int start, int pageLength , System.Int32? idUsuario, System.Int32? idRol);
		
		#endregion
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;PatMenu&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;PatMenu&gt;"/></returns>
		public static TList<PatMenu> Fill(IDataReader reader, TList<PatMenu> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				PAT.Entities.PatMenu c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("PatMenu")
					.Append("|").Append((System.Int16)reader[((int)PatMenuColumn.IdMenu - 1)]).ToString();
					c = EntityManager.LocateOrCreate<PatMenu>(
					key.ToString(), // EntityTrackingKey
					"PatMenu",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new PAT.Entities.PatMenu();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.IdMenu = (System.Int16)reader[((int)PatMenuColumn.IdMenu - 1)];
					c.IdMenuPadre = (reader.IsDBNull(((int)PatMenuColumn.IdMenuPadre - 1)))?null:(System.Int16?)reader[((int)PatMenuColumn.IdMenuPadre - 1)];
					c.Descripcion = (System.String)reader[((int)PatMenuColumn.Descripcion - 1)];
					c.Url = (reader.IsDBNull(((int)PatMenuColumn.Url - 1)))?null:(System.String)reader[((int)PatMenuColumn.Url - 1)];
					c.UrlHelp = (System.String)reader[((int)PatMenuColumn.UrlHelp - 1)];
					c.Activo = (System.Boolean)reader[((int)PatMenuColumn.Activo - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatMenu"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatMenu"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, PAT.Entities.PatMenu entity)
		{
			if (!reader.Read()) return;
			
			entity.IdMenu = (System.Int16)reader[((int)PatMenuColumn.IdMenu - 1)];
			entity.IdMenuPadre = (reader.IsDBNull(((int)PatMenuColumn.IdMenuPadre - 1)))?null:(System.Int16?)reader[((int)PatMenuColumn.IdMenuPadre - 1)];
			entity.Descripcion = (System.String)reader[((int)PatMenuColumn.Descripcion - 1)];
			entity.Url = (reader.IsDBNull(((int)PatMenuColumn.Url - 1)))?null:(System.String)reader[((int)PatMenuColumn.Url - 1)];
			entity.UrlHelp = (System.String)reader[((int)PatMenuColumn.UrlHelp - 1)];
			entity.Activo = (System.Boolean)reader[((int)PatMenuColumn.Activo - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatMenu"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatMenu"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, PAT.Entities.PatMenu entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IdMenu = (System.Int16)dataRow["ID_MENU"];
			entity.IdMenuPadre = Convert.IsDBNull(dataRow["ID_MENU_PADRE"]) ? null : (System.Int16?)dataRow["ID_MENU_PADRE"];
			entity.Descripcion = (System.String)dataRow["DESCRIPCION"];
			entity.Url = Convert.IsDBNull(dataRow["URL"]) ? null : (System.String)dataRow["URL"];
			entity.UrlHelp = (System.String)dataRow["URL_HELP"];
			entity.Activo = (System.Boolean)dataRow["ACTIVO"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatMenu"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">PAT.Entities.PatMenu Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, PAT.Entities.PatMenu entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region IdMenuPadreSource	
			if (CanDeepLoad(entity, "PatMenu|IdMenuPadreSource", deepLoadType, innerList) 
				&& entity.IdMenuPadreSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.IdMenuPadre ?? (short)0);
				PatMenu tmpEntity = EntityManager.LocateEntity<PatMenu>(EntityLocator.ConstructKeyFromPkItems(typeof(PatMenu), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.IdMenuPadreSource = tmpEntity;
				else
					entity.IdMenuPadreSource = DataRepository.PatMenuProvider.GetByIdMenu(transactionManager, (entity.IdMenuPadre ?? (short)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdMenuPadreSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdMenuPadreSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.PatMenuProvider.DeepLoad(transactionManager, entity.IdMenuPadreSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion IdMenuPadreSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIdMenu methods when available
			
			#region IdRolPatRolCollection_From_PatRolMenu
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<PatRol>|IdRolPatRolCollection_From_PatRolMenu", deepLoadType, innerList))
			{
				entity.IdRolPatRolCollection_From_PatRolMenu = DataRepository.PatRolProvider.GetByIdMenuFromPatRolMenu(transactionManager, entity.IdMenu);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdRolPatRolCollection_From_PatRolMenu' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdRolPatRolCollection_From_PatRolMenu != null)
				{
					deepHandles.Add("IdRolPatRolCollection_From_PatRolMenu",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< PatRol >) DataRepository.PatRolProvider.DeepLoad,
						new object[] { transactionManager, entity.IdRolPatRolCollection_From_PatRolMenu, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region PatMenuCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatMenu>|PatMenuCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatMenuCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatMenuCollection = DataRepository.PatMenuProvider.GetByIdMenuPadre(transactionManager, entity.IdMenu);

				if (deep && entity.PatMenuCollection.Count > 0)
				{
					deepHandles.Add("PatMenuCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatMenu>) DataRepository.PatMenuProvider.DeepLoad,
						new object[] { transactionManager, entity.PatMenuCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region PatRolMenuCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatRolMenu>|PatRolMenuCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatRolMenuCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatRolMenuCollection = DataRepository.PatRolMenuProvider.GetByIdMenu(transactionManager, entity.IdMenu);

				if (deep && entity.PatRolMenuCollection.Count > 0)
				{
					deepHandles.Add("PatRolMenuCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatRolMenu>) DataRepository.PatRolMenuProvider.DeepLoad,
						new object[] { transactionManager, entity.PatRolMenuCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the PAT.Entities.PatMenu object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">PAT.Entities.PatMenu instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">PAT.Entities.PatMenu Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, PAT.Entities.PatMenu entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region IdMenuPadreSource
			if (CanDeepSave(entity, "PatMenu|IdMenuPadreSource", deepSaveType, innerList) 
				&& entity.IdMenuPadreSource != null)
			{
				DataRepository.PatMenuProvider.Save(transactionManager, entity.IdMenuPadreSource);
				entity.IdMenuPadre = entity.IdMenuPadreSource.IdMenu;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region IdRolPatRolCollection_From_PatRolMenu>
			if (CanDeepSave(entity.IdRolPatRolCollection_From_PatRolMenu, "List<PatRol>|IdRolPatRolCollection_From_PatRolMenu", deepSaveType, innerList))
			{
				if (entity.IdRolPatRolCollection_From_PatRolMenu.Count > 0 || entity.IdRolPatRolCollection_From_PatRolMenu.DeletedItems.Count > 0)
				{
					DataRepository.PatRolProvider.Save(transactionManager, entity.IdRolPatRolCollection_From_PatRolMenu); 
					deepHandles.Add("IdRolPatRolCollection_From_PatRolMenu",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<PatRol>) DataRepository.PatRolProvider.DeepSave,
						new object[] { transactionManager, entity.IdRolPatRolCollection_From_PatRolMenu, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<PatMenu>
				if (CanDeepSave(entity.PatMenuCollection, "List<PatMenu>|PatMenuCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatMenu child in entity.PatMenuCollection)
					{
						if(child.IdMenuPadreSource != null)
						{
							child.IdMenuPadre = child.IdMenuPadreSource.IdMenu;
						}
						else
						{
							child.IdMenuPadre = entity.IdMenu;
						}

					}

					if (entity.PatMenuCollection.Count > 0 || entity.PatMenuCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatMenuProvider.Save(transactionManager, entity.PatMenuCollection);
						
						deepHandles.Add("PatMenuCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatMenu >) DataRepository.PatMenuProvider.DeepSave,
							new object[] { transactionManager, entity.PatMenuCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<PatRolMenu>
				if (CanDeepSave(entity.PatRolMenuCollection, "List<PatRolMenu>|PatRolMenuCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatRolMenu child in entity.PatRolMenuCollection)
					{
						if(child.IdMenuSource != null)
						{
								child.IdMenu = child.IdMenuSource.IdMenu;
						}

						if(child.IdRolSource != null)
						{
								child.IdRol = child.IdRolSource.IdRol;
						}

					}

					if (entity.PatRolMenuCollection.Count > 0 || entity.PatRolMenuCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatRolMenuProvider.Save(transactionManager, entity.PatRolMenuCollection);
						
						deepHandles.Add("PatRolMenuCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatRolMenu >) DataRepository.PatRolMenuProvider.DeepSave,
							new object[] { transactionManager, entity.PatRolMenuCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region PatMenuChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>PAT.Entities.PatMenu</c>
	///</summary>
	public enum PatMenuChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>PatMenu</c> at IdMenuPadreSource
		///</summary>
		[ChildEntityType(typeof(PatMenu))]
		PatMenu,
	
		///<summary>
		/// Collection of <c>PatMenu</c> as ManyToMany for PatRolCollection_From_PatRolMenu
		///</summary>
		[ChildEntityType(typeof(TList<PatRol>))]
		IdRolPatRolCollection_From_PatRolMenu,

		///<summary>
		/// Collection of <c>PatMenu</c> as OneToMany for PatMenuCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatMenu>))]
		PatMenuCollection,

		///<summary>
		/// Collection of <c>PatMenu</c> as OneToMany for PatRolMenuCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatRolMenu>))]
		PatRolMenuCollection,
	}
	
	#endregion PatMenuChildEntityTypes
	
	#region PatMenuFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;PatMenuColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatMenu"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatMenuFilterBuilder : SqlFilterBuilder<PatMenuColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatMenuFilterBuilder class.
		/// </summary>
		public PatMenuFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatMenuFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatMenuFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatMenuFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatMenuFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatMenuFilterBuilder
	
	#region PatMenuParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;PatMenuColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatMenu"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatMenuParameterBuilder : ParameterizedSqlFilterBuilder<PatMenuColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatMenuParameterBuilder class.
		/// </summary>
		public PatMenuParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatMenuParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatMenuParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatMenuParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatMenuParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatMenuParameterBuilder
	
	#region PatMenuSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;PatMenuColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatMenu"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class PatMenuSortBuilder : SqlSortBuilder<PatMenuColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatMenuSqlSortBuilder class.
		/// </summary>
		public PatMenuSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion PatMenuSortBuilder
	
} // end namespace
